package com.nit.lab.mapper.basic;

import com.nit.lab.dto.basic.TeacherDTO;
import com.nit.lab.entity.basic.TeacherEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @文件名：TeacherMapper
 * @创建人: wangsheng
 * @创建时间: 2020/3/14
 * @描述:
 */
public interface TeacherMapper {
    /**
     * @描述 获取数据表格
     * @创建人 ybj
     * @创建时间 2020/3/28
     */
    @Select({"<script>SELECT " +
            "teacher_id AS `key`, teacher_id, " +
            "teacher_code, role_id, school_id, " +
            "teacher_name, teacher_phone, " +
            "teacher_info, gmt_create " +
            "FROM basic_teacher " +
            "WHERE 1 = 1 AND is_deleted = 0 " +
            "<if test='teacherCode != null'> AND teacher_code LIKE CONCAT('%', #{teacherCode}, '%') </if>" +
            "<if test='teacherName != null'> AND teacher_name LIKE CONCAT('%', #{teacherName}, '%') </if>" +
            "<if test='teacherPhone != null'> AND teacher_phone LIKE CONCAT('%', #{teacherPhone} '%') </if>" +
            "ORDER BY " +
            "<if test='sortField != null'>" +
            "${sortField} ${sortOrder}, " +
            "</if>" +
            "teacher_id desc " +
            "LIMIT #{start}, #{length}" +
            "</script>"})
    List<TeacherDTO> list4Table(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("teacherCode") String teacherCode,
            @Param("teacherName") String teacherName,
            @Param("teacherPhone") String teacherPhone,
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder) throws Exception;

    /**
     * @描述 获取数据表格的总数
     * @创建人 ybj
     * @创建时间 2020/3/28
     */
    @Select({"<script>SELECT COUNT(*) " +
            "FROM basic_teacher " +
            "WHERE 1 = 1 AND is_deleted = 0 " +
            "<if test='teacherCode != null'> AND teacher_code LIKE CONCAT('%', #{teacherCode}, '%') </if>" +
            "<if test='teacherName != null'> AND teacher_name LIKE CONCAT('%', #{teacherName}, '%') </if>" +
            "<if test='teacherPhone != null'> AND teacher_phone LIKE CONCAT('%', #{teacherPhone} '%') </if>" +
            "</script>"})
    Long count4Table(
            @Param("teacherCode") String teacherCode,
            @Param("teacherName") String teacherName,
            @Param("teacherPhone") String teacherPhone) throws Exception;

    /**
     * @描述 添加教师信息
     * @创建人 ybj
     * @创建时间 2020/3/28
     */
    @Insert("INSERT INTO basic_teacher (" +
            "teacher_code, teacher_pwd, " +
            "teacher_name, teacher_phone, " +
            "teacher_info, role_id, school_id, gmt_create) " +
            "VALUES (#{teacherEntity.teacherCode}, #{teacherEntity.teacherPwd}, " +
            "#{teacherEntity.teacherName}, #{teacherEntity.teacherPhone}, " +
            "#{teacherEntity.teacherInfo}, #{teacherEntity.roleId}, " +
            "#{teacherEntity.schoolId},  #{teacherEntity.gmtCreate})")
    @Options(useGeneratedKeys = true, keyProperty = "teacherId", keyColumn = "teacher_id")
    Integer add(@Param("teacherEntity") TeacherEntity teacherEntity) throws Exception;

    /**
     * @描述 验证TeacherCode的唯一性
     * @创建人 ybj
     * @创建时间 2020/3/28
     */
    @Select({"SELECT * FROM basic_teacher WHERE teacher_code = #{teacherCode} AND is_deleted = 0 LIMIT 1"})
    TeacherDTO getByLoginName(@Param("teacherCode") String teacherCode) throws Exception;

    /**
     * @描述 编辑教师信息
     * @创建人 ybj
     * @创建时间 2020/3/28
     */
    @Update("UPDATE basic_teacher " +
            "SET teacher_code = #{teacherEntity.teacherCode}, " +
            "teacher_name = #{teacherEntity.teacherName}, " +
            "teacher_phone = #{teacherEntity.teacherPhone}, " +
            "teacher_info = #{teacherEntity.teacherInfo}, " +
            "role_id = #{teacherEntity.roleId} " +
            "WHERE teacher_id = #{teacherEntity.teacherId}")
    Integer update(@Param("teacherEntity") TeacherEntity teacherEntity) throws Exception;

    /**
     * @描述 删除教师信息
     * @创建人 ybj
     * @创建时间 2020/3/28
     */
    @Update("UPDATE basic_teacher SET is_deleted = 1 WHERE teacher_id = #{teacherId}")
    Integer remove(@Param("teacherId") Long teacherId) throws Exception;

    /**
     * @描述 更改密码
     * @创建人 ybj
     * @创建时间 2020/3/28
     */
    @Update({"UPDATE basic_teacher " +
            "SET teacher_pwd = #{teacherPwd} " +
            "WHERE teacher_id = #{teacherId}"
    })
    Integer resetPwd(@Param("teacherId") Long teacherId,
                     @Param("teacherPwd") String teacherPwd) throws Exception;

    /**
     * @Description: 根据Id获取信息
     * @Author: lxr
     * @Date: 2020/03/25
     */
    @Select({"SELECT * " +
            "FROM basic_teacher a " +
            "join system_school b " +
            "on a.school_id = b.school_id " +
            "WHERE a.teacher_id = #{teacherId} " +
            "AND a.is_deleted = 0 "})
    TeacherDTO getByTeacherId(@Param("teacherId") Long teacherId) throws Exception;

    @Select("SELECT teacher_id, teacher_name " +
            "FROM basic_teacher " +
            "WHERE TRUE AND is_deleted = 0 ")
    @Results({
            @Result(column = "teacher_id", property = "teacherId"),
            @Result(column = "teacher_name", property = "teacherName")
    })
    List<TeacherDTO> listTeacher() throws Exception;
}
